<?php
class ome_finder_shop{
    var $detail_shop = "前端店铺详情";
    var $detail_config = "前端店铺同步";
    //增加前端店铺回写设置
    var $detail_request = "前端回写设置";
    //店铺启用API
    var $detail_api = "店铺启用API";

    function detail_shop($shop_id){
        $render = app::get('ome')->render();
        $oShop = &app::get('ome')->model("shop");
        $shop = $oShop->dump($shop_id);
        $shop_type = ome_shop_type::get_shop_type();
        $reset_login = "";
        $shoptype = $shop['node_type'];
        $shopid = $shop['shop_id'];
        $node_id = $shop['node_id'];
        $finder_id = $_GET['_finder']['finder_id'];
        $taobao_session = &app::get('ome')->getConf('taobao_session_'.$node_id);
        $taobao_session = strval($taobao_session);
        $certi_id = base_certificate::get('certificate_id');
        
        $api_url = kernel::base_url(true).kernel::url_prefix().'/api';
       // $api_url = urlencode(kernel::openapi_url('openapi.ome.shop','session_callback',array('shop_id'=>$shop_id)));
        $url = OPENID_URL.APPKEY."&node_id=$node_id&callback_url=".$api_url;
         if ($shoptype=='taobao' and $node_id and ($taobao_session=='true' or $taobao_session==1)){
            $reset_login = '<a href="'.$url.'" target="_blank"><b>重新登录</a></a>';
        }
        $render->pagedata['reset_login'] = $reset_login;
        $render->pagedata['shop']=$shop;
        $render->pagedata['finder_id']=$finder_id;
        $render->pagedata['shop_type'] = $shop_type;
        return $render->fetch("admin/system/terminal_detail.html");

    }

    /**
     *前端店铺同步
     *
     * @param String $shop_id
     */
    function detail_config($shop_id){
        $render = app::get('ome')->render();
        //$oShop = &app::get('ome')->model("shop");
        //$shop = $oShop->dump($shop_id);
        //$render->pagedata['shop'] = $shop;
        $render->pagedata['shop_id'] = $shop_id;
        $render->pagedata['sync_interval'] = defined('SYNC_ORDER_INTERVAL') ? SYNC_ORDER_INTERVAL : 60;

        return $render->fetch("admin/system/shop_config_detail.html");
    }

    function detail_request($shop_id) {

        $request_auto_stock = app::get('ome')->getConf('shop_stock_config_' . $shop_id);

        //如无设置,缺省置为 true
        if (empty($request_auto_stock)) {
            $request_auto_stock = 'false';
            app::get('ome')->setConf('shop_stock_config_' . $shop_id, 'true');
        }

        $render = app::get('ome')->render();
        $render->pagedata['request_auto_stock'] = $request_auto_stock;
        $render->pagedata['shop_id'] = $shop_id;
        return $render->fetch("admin/system/terminal_request.html");
    }
    /**
     * 店铺api
     * @param unknown_type $shop_id
     */
    function detail_api($shop_id) {
    	$render = app::get('ome')->render();
    	$render->pagedata['shop_id'] = $shop_id;
    	return $render->fetch("admin/system/terminal_api.html");
    }

    var $addon_cols = "shop_id,shop_type,node_id,name,node_type";
    var $column_edit = "操作";
    var $column_edit_width = "220";
    function column_edit($row){

        $finder_id = $_GET['_finder']['finder_id'];
        $shop_type = $row[$this->col_prefix.'shop_type'];
        $node_type = $row[$this->col_prefix.'node_type'];
        $node_id = $row[$this->col_prefix.'node_id'];
        $shop_id = $row[$this->col_prefix.'shop_id'];
        $shop_name = $row[$this->col_prefix.'name'];
        $button1 = '<a href="index.php?app=ome&ctl=admin_shop&act=editterminal&p[0]='.$row[$this->col_prefix.'shop_id'].'&finder_id='.$finder_id.'" target="_blank">编辑</a>';
        $taobao_session = &app::get('ome')->getConf('taobao_session_'.$node_id);
        $taobao_session  =  $taobao_session ? $taobao_session : 'false';
        $certi_id = base_certificate::get('certificate_id');
        $api_url = kernel::base_url(true).kernel::url_prefix().'/api?node_id='.$node_id;
       // $api_url = urlencode(kernel::openapi_url('openapi.ome.shop','session_callback',array('shop_id'=>$shop_id)));
        $api_url = kernel::base_url(true).kernel::url_prefix().'/api';
        $url = OPENID_URL.APPKEY."&node_id=$node_id&callback_url=".$api_url;
        if ($shop_type=='taobao' and $node_id and ($taobao_session=='false' or $taobao_session=='False' or $taobao_session==false) ){
            $button2 = ' | <a href="'.$url.'" target="_blank">登录淘宝</a>';
        }
        $callback_url = urlencode(kernel::openapi_url('openapi.ome.shop','shop_callback',array('shop_id'=>$shop_id)));
        $app_id = "ome";
        $api_url = urlencode($api_url);
        if (!$node_id) $button3 = ' | <a href="index.php?app=ome&ctl=admin_shop&act=apply_bindrelation&p[0]='.$app_id.'&p[1]='.$callback_url.'&p[2]='.$api_url.'" target="_blank">申请绑定</a>';
        else $button3 = ' | 已绑定';

        //扩展操作功能按钮
        if ($extend_button_service = kernel::servicelist('ome.shop.finder')){
            foreach($extend_button_service as $object=>$instance){
                if(method_exists($instance, 'operator_button')){
                    $extend_button .= ' | '.$instance->operator_button($shop_id,$shop_name,$node_id,$node_type);
                }
            }
        }
        if (strlen($extend_button) < '6'){
            $extend_button = '';
        }
        return $button1.$button3.$button2.$extend_button;
    }
}
?>